const MovieHttp=require("../../utils/MovieHttp")
Page({
    data: {
        banner:"",
        top:"",
        sum:"",
        coming:"",
        comings:"",
        inTheaters:""
    },
    onLoad:async function (options) {
        var topRes=await MovieHttp.getTop();
        var num=topRes.data.res.slice(0,3);
        num.forEach(item=>{
            var sum=handleStar(item.rating);
            this.setData({sum})
        })

        var comingSoon=await MovieHttp.getComingSoon();
        var coming=comingSoon.data.res.slice(0,3);
        coming.forEach(item=>{
            var comings=handleStar(item.rating);
            this.setData({comings})
        })

        var inTheaters=await MovieHttp.getInTheaters();
        var theater=inTheaters.data.res.slice(0,3);
        theater.forEach(item=>{
            var theater=handleStar(item.raiting);
            this.setData({theater})
        })
        
        var banner=await MovieHttp.getBanner();
        this.setData({
            banner:banner.data.banners,
            top:topRes.data.res.slice(0,3),
            coming:comingSoon.data.res.slice(0,3),
            inTheater:inTheaters.data.res.slice(0,3)
            })    
    function handleStar(num){
    var arr=[];
    for(var i=0;i<5;i++){
        if(num>=2){
            arr.push(2)
        }else if(num>0){
            arr.push(Number(num.toFixed(1)))
        }else{
            arr.push(0)
        }num=num-2
    }
    return arr
    }
    
    },
    
})